package com.briup.labs.service.impl;

import com.briup.labs.mapper.SmartDeviceMapper;
import com.briup.labs.pojo.*;
import com.briup.labs.service.SmartDeviceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class SmartDeviceServiceImpl implements SmartDeviceService {

    @Autowired
    private SmartDeviceMapper smartDeviceMapper;

    @Override
    public PageBean<SmartDevice> list(String pageNum, String pageSize, String key, String typeId) {
        //创建PageBean对象
        PageBean<SmartDevice> pageBean = new PageBean<>();

        //检测数据输入格式
        int PN = 0;
        int PS = 0;
        try {
            PN = Integer.parseInt(pageNum);
            PS = Integer.parseInt(pageSize);
        } catch (Exception e) {
            pageBean.setTotal(null);
            pageBean.setItems(null);
            return pageBean;
        }

        //开启分页查询 PageHelper
        PageHelper.startPage(PN, PS);

        //调用mapper
        List<SmartDevice> smartDevices = smartDeviceMapper.all(key, typeId);

        //创建最后的数据集合
        List<Device> list = new ArrayList<>();
        for (SmartDevice smartDevice : smartDevices) {
            Device device = new Device();
            //加入设备数据
            device.setId(smartDevice.getId());
            device.setDeviceName(smartDevice.getDeviceName());
            device.setDeviceAddr(smartDevice.getDeviceAddr());
            device.setDeviceType(smartDevice.getDeviceType());
            device.setLabsId(smartDevice.getLabsId());
            device.setOpen(smartDevice.getOpen());
            device.setClose(smartDevice.getClose());
            device.setMaxThreshold(smartDevice.getMaxThreshold());
            device.setMinThreshold(smartDevice.getMinThreshold());
            //加入类型数据
            SmartDeviceType smartDeviceType = smartDeviceMapper.getType(smartDevice.getDeviceType());
            device.setSmartDeviceType(smartDeviceType);
            //加入实验室数据
            SmartLabs smartLabs = smartDeviceMapper.getLabs(smartDevice.getLabsId());
            device.setSmartLabs(smartLabs);
            list.add(device);
        }

        //Page中提供了方法，可以获取PageHelper分页查询后，得到的总记录条数和当夜数据
        //Page<Labs> page = (Page<Labs>) list;

        //Page不可用，改用PageInfo
        PageInfo<SmartDevice> pageInfo = new PageInfo<>(list);

        //把数据填充到PageBean对象中
        pageBean.setTotal(pageInfo.getTotal());
        pageBean.setItems(pageInfo.getList());
        return pageBean;
    }

    @Override
    public List<SmartDevice> list(String key, String typeId) {
        return smartDeviceMapper.all(key, typeId);
    }

    @Override
    public void saveOrUpdate(SmartDevice smartDevice) {
        //数据表没有设置自增
        List<SmartDevice> smartDevices = smartDeviceMapper.all(null, null);
        for (SmartDevice device : smartDevices) {
            if (Objects.equals(smartDevice.getId(), device.getId())) {
                //修改
                smartDeviceMapper.update(smartDevice);
                return;
            }
        }
        //添加
        smartDeviceMapper.add(smartDevice);
    }

    @Override
    public void delete(String id) {
        smartDeviceMapper.delete(id);
    }
}
